Add statsd graceful shutdown support #42343
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have encountered an issue in my Spring Batch job where, intermittently, the error "Singleton bean creation not allowed while the singletons of this factory are in destruction" occurs with the StatsdMeterRegistry bean.
This issue seems to arise when the application unexpectedly receives a SIGTERM signal (such as when a node goes down in a cloud environment) and begins the shutdown process, some background tasks in the StatsdMeterRegistry are not properly terminated before the bean destruction phase. As a result, there appears to be an attempt to access the StatsdMeterRegistry after the ApplicationContext has already started to shut down.
To address this, I believe it would be beneficial to gracefully shut down the StatsdMeterRegistry before the bean is destroyed. A possible solution could be to modify the StatsdMetricsExportAutoConfiguration to ensure that StatsdMeterRegistry.close() is called during the shutdown phase, as shown below:
This would help to properly terminate any ongoing background tasks and prevent the issue from occurring during application shutdown.
Thank you for considering this PR. I look forward to your feedback!